﻿//准备ExtJS环境
Ext.require
(
    [
        'Ext.form.field.File',
        'Ext.grid.*',
        'Ext.data.*',
        'Ext.util.*',
        'Ext.state.*',
        'Ext.form.*'
    ]
);

//定义用户表格控件
Ext.define
(
    'Instruction.Grid',
    {
        extend: 'Ext.grid.Panel',

        //编辑窗体
        win_edit: null,

        //新增窗体
        win_create: null,

        //初始化组件
        initComponent: function ()
        {
            Ext.apply
            (
                this,
                {
                    listeners:
                    {
                        //设置两个指头的滚动
                        render: Common.ScrollableRender
                    },
                    dockedItems:
                    [
                        {
                            xtype: 'toolbar',
                            height: 36,
                            items:
                            [
                                {
                                    iconCls: 'icon-refresh',
                                    text: '刷新',
                                    scope: this,
                                    hidden: false,
                                    tooltip: '刷新数据',
                                    handler: this.onRefreshClick
                                },
                                '-',
                                {
                                    iconCls: 'icon-add',
                                    text: '新增',
                                    scope: this,
                                    hidden: false,
                                    tooltip: '新增指令',
                                    handler: this.onAddClick
                                },
                                {
                                    iconCls: 'icon-delete',
                                    text: '删除',
                                    disabled: true,
                                    hidden: false,
                                    itemId: 'button-delete',
                                    scope: this,
                                    tooltip: '删除指令',
                                    handler: this.onDeleteClick
                                },
                                {
                                    iconCls: 'icon-edit',
                                    text: '编辑',
                                    itemId: 'button-edit',
                                    disabled: true,
                                    hidden: false,
                                    scope: this,
                                    tooltip: '编辑指令',
                                    handler: this.onEditClick
                                },
                                {
                                    iconCls: 'icon-view',
                                    itemId: 'button-view',
                                    text: '查看',
                                    scope: this,
                                    disabled: true,
                                    hidden: false,
                                    tooltip: '查看突发事件',
                                    handler: this.onViewClick
                                },
                                '->',
                                {
                                    iconCls: 'icon-submit',
                                    text: '确认发布',
                                    itemId: 'button-submit',
                                    scope: this,
                                    disabled: true,
                                    hidden: false,
                                    tooltip: '确认发布',
                                    handler: this.onSubmitClick
                                }
                            ]
                        }
                    ]
                }
            );
            this.callParent();
            this.getSelectionModel().on('selectionchange', this.onSelectionChange, this);
        },

        //处理选中养护变化事件
        onSelectionChange: function (selectionModel, selections)
        {
            //判断是否选中养护
            if (selections.length === 0)
            {
                //禁用删除按钮
                this.down('#button-delete').setDisabled(true);
                //禁用编辑按钮
                this.down('#button-edit').setDisabled(true);
                //禁用上传按钮
                this.down('#button-submit').setDisabled(true);
                //禁用查看按钮
                this.down('#button-view').setDisabled(true);
            }
            else
            {
                //启用删除按钮
                this.down('#button-delete').setDisabled(false);
                //启用编辑按钮
                this.down('#button-edit').setDisabled(false);
                //启用上传按钮
                this.down('#button-submit').setDisabled(false);
                //启用查看按钮
                this.down('#button-view').setDisabled(false);
            }
        },

        //处理删除按钮点击事件
        onDeleteClick: function ()
        {
            //获得数据存储对象
            var store = this.store;

            //获得选中的行
            var selection = this.getView().getSelectionModel().getSelection()[0];

            //判断是否选中行
            if (selection)
            {
                //询问是否确定删除
                Ext.Msg.show
                (
                    {
                        title: '删除指令',
                        width: 320,
                        height: 240,
                        msg: '确定要删除选中的指令"' + selection.data.Title + '"吗?',
                        buttons: Ext.Msg.YESNO,
                        icon: Ext.Msg.QUESTION,
                        fn: function (button)
                        {
                            //判断是否确定删除 
                            if (button == 'yes')
                            {
                                //创建请求对象
                                Ext.Ajax.request
                                (
                                    {
                                        url: Common.MakeAbsoluteUrl('Delete'),
                                        method: HttpMethod.Post,
                                        params: selection.data,
                                        callback: function (options, success, response)
                                        {
                                            //处理错误
                                            if (Common.HandleError(options, success, response))
                                            {
                                                //刷新数据
                                                store.load();
                                            }
                                        }
                                    }
                                );
                            }
                        }
                    }
                );
            }
        },

        //处理刷新按钮点击事件
        onRefreshClick: function ()
        {
            //刷新数据
            this.store.load();
        },

        //处理查看按钮点击事件
        onViewClick: function ()
        {
            //获得选中的行
            var selection = this.getView().getSelectionModel().getSelection();

            //判断是否选中行
            if (selection.length > 0)
            {
                //查看工程详细信息
                window.open(Common.MakeAbsoluteUrl('ViewInstruction?') + 'instructionID=' + selection[0].data.InstructionID);
            }
        },

        //处理新增按钮点击事件
        onAddClick: function ()
        {
            //获得数据存储对象
            var store = this.store;

            //判定窗体是否创建
            if (!this.win_create)
            {
                //创建窗体
                this.win_create = new Ext.Window
                (
                {
                    title: '新增指令',
                    maximizable: false,
                    maximized: false,
                    width: 800,
                    height: 600,
                    bodyBorder: false,
                    draggable: true,
                    isTopContainer: true,
                    modal: true,
                    resizable: false,
                    bodyPadding: 0,
                    closeAction: 'hide',
                    closable: false,
                    layout: 'fit',
                    fieldDefaults:
                    {
                        labelAlign: 'left',
                        labelWidth: 72,
                        anchor: '100%'
                    },
                    listeners:
                    {
                        //挂接显示事件
                        show: function (win, options)
                        {
                            //获得表单对象
                            var form = win.down('form').getForm();
                            //重置表单
                            form.reset();
                            //获得控件
                            var field = win.down('#txtContent');

                            //退出源码模式
                            field.toggleSourceEdit(false);
                        }
                    },
                    items:
                    [
                        {
                            xtype: 'form',
                            method: 'POST',
                            waitMsgTarget: true,
                            waitTitle: Common.ApplicationName,
                            frame: false,
                            bodyPadding: 12,
                            bodyStyle: 'background-color:White;margin:0;',
                            bodyBorder: false,
                            border: 0,
                            autoScroll: true,
                            defaultType: 'textfield',
                            autoScroll: false,
                            fieldDefaults:
                            {
                                labelAlign: 'left',
                                labelWidth: 72,
                                anchor: '100%',
                                msgTarget: 'qtip',
                                autoFitErrors: true,
                                disabledCls: ''
                            },
                            items:
                            [
                                {
                                    xtype: 'textfield',
                                    itemId: 'txtTitle',
                                    name: 'Title',
                                    fieldLabel: '指令主题',
                                    emptyText: '80个字符',
                                    allowBlank: false,
                                    maxLength: 80
                                },
                                {
                                    xtype: 'htmleditor',
                                    name: 'Content',
                                    itemId: 'txtContent',
                                    stateful: true,
                                    fieldLabel: '指令说明',
                                    height: 280,
                                    allowBlank: false,
                                    autoScroll: false,
                                    fontFamilies: ['宋体', '新宋体', '黑体', '楷体', '隶书', '幼圆', '仿宋']
                                },
                                {
                                    xtype: 'radiogroup',
                                    fieldLabel: '指令类型',
                                    columns: 6,
                                    items:
                                    [
                                        {
                                            boxLabel: '工程指令',
                                            name: 'InstructionType',
                                            inputValue: 0,
                                            checked: true
                                        },
                                        {
                                            boxLabel: '养护指令',
                                            name: 'InstructionType',
                                            inputValue: 1
                                        }
                                    ]
                                },
                                {
                                    xtype: 'radiogroup',
                                    fieldLabel: '发送范围',
                                    columns: 6,
                                    items:
                                    [
                                        {
                                            boxLabel: '所有单位',
                                            name: 'PostType',
                                            inputValue: 0,
                                            checked: true,
                                            listeners:
                                            {
                                                //处理变化事件
                                                change: function (field, newValue, oldValue, eOpts)
                                                {
                                                    //获得控件
                                                    var chkReceivers = field.up('form').down('#chkReceivers');

                                                    //设置控件状态
                                                    chkReceivers.setDisabled(newValue);
                                                }
                                            }
                                        },
                                        {
                                            boxLabel: '指定单位',
                                            name: 'PostType',
                                            inputValue: 1,
                                            checked: false
                                        }
                                    ]
                                },
                                {
                                    xtype: 'checkboxgroup',
                                    itemId: 'chkReceivers',
                                    fieldLabel: '接收单位',
                                    disabled: true,
                                    border: 1,
                                    columns: 4,
                                    vertical: false,
                                    autoScroll: true,
                                    items: master_organization,
                                    height: 130,
                                    maxHeight: 130
                                }
                            ],
                            buttons:
                            [
                                {
                                    text: '确定',
                                    height: 26,
                                    margin: 3,
                                    handler: function ()
                                    {
                                        //获得表单对象
                                        var form = this.up('form').getForm();

                                        //检查数据是否合法
                                        if (form.isValid())
                                        {
                                            //提交数据
                                            form.submit
                                            (
                                                {
                                                    url: Common.MakeAbsoluteUrl('Create'),
                                                    method: HttpMethod.Post,
                                                    scope: this.up('window'),
                                                    submitEmptyText: false,
                                                    waitTitle: '请稍候',
                                                    waitMsg: '正在保存数据...',
                                                    success: function (form, action)
                                                    {
                                                        //处理提交错误
                                                        if (Common.HandleFormError(form, action))
                                                        {
                                                            //重置数据
                                                            form.reset();
                                                            //隐藏窗体
                                                            this.hide();
                                                            //刷新数据
                                                            store.load();
                                                        }
                                                    },
                                                    failure: Common.HandleFormFailure
                                                }
                                            );
                                        }
                                    }
                                },
                                {
                                    text: '取消',
                                    height: 26,
                                    margin: 3,
                                    handler: function ()
                                    {
                                        //获得表单对象
                                        var form = this.up('form').getForm();
                                        //重置数据
                                        form.reset();
                                        //隐藏窗体
                                        this.up('window').hide();
                                    }
                                }
                            ]
                        }
                    ]
                }
             );
            }

            //显示窗体
            this.win_create.show();
        },

        //处理编辑按钮点击事件
        onEditClick: function ()
        {
            //获得数据存储对象
            var store = this.store;

            //获得选中的行
            var selection = this.getView().getSelectionModel().getSelection()[0];

            //判断是否选中行
            if (selection)
            {
                //判定窗体是否已经创建
                if (!this.win_edit)
                {
                    //创建窗体
                    this.win_edit = new Ext.Window
                    (
                        {
                            title: '编辑指令',
                            maximizable: false,
                            maximized: false,
                            width: 800,
                            height: 600,
                            autoScroll: false,
                            bodyBorder: false,
                            draggable: true,
                            isTopContainer: true,
                            modal: true,
                            resizable: false,
                            bodyPadding: 0,
                            closeAction: 'hide',
                            closable: false,
                            layout: 'fit',
                            fieldDefaults:
                            {
                                labelAlign: 'left',
                                labelWidth: 72,
                                anchor: '100%'
                            },
                            listeners:
                            {
                                //挂接显示事件
                                show: function (win, options)
                                {
                                    //获得表单对象
                                    var form = win.down('form').getForm();

                                    //获得控件
                                    var field = win.down('#txtContent');

                                    //退出源码模式
                                    field.toggleSourceEdit(false);

                                    //加载数据
                                    form.load
                                    (
                                        {
                                            url: Common.MakeAbsoluteUrl('Details?instructionID=' + win.current_row.InstructionID),
                                            waitMsg: '数据加载中...',
                                            failure: function (form, action)
                                            {
                                                //关闭窗体
                                                win.hide();

                                                //处理错误
                                                Common.HandleFormFailure(form, action);
                                            }
                                        }
                                    );
                                }
                            },
                            items:
                            [
                                {
                                    xtype: 'form',
                                    method: 'POST',
                                    waitMsgTarget: true,
                                    waitTitle: Common.ApplicationName,
                                    frame: false,
                                    bodyPadding: 12,
                                    bodyStyle: 'background-color:White;margin:0;',
                                    bodyBorder: false,
                                    border: 0,
                                    autoScroll: true,
                                    defaultType: 'textfield',
                                    autoScroll: false,
                                    fieldDefaults:
                                    {
                                        labelAlign: 'left',
                                        labelWidth: 72,
                                        anchor: '100%',
                                        msgTarget: 'qtip',
                                        autoFitErrors: true,
                                        disabledCls: ''
                                    },
                                    items:
                                    [
                                        {
                                            xtype: 'hiddenfield',
                                            name: 'InstructionID',
                                            allowBlank: false
                                        },
                                        {
                                            xtype: 'textfield',
                                            itemId: 'txtTitle',
                                            name: 'Title',
                                            fieldLabel: '指令主题',
                                            emptyText: '80个字符',
                                            allowBlank: false,
                                            maxLength: 80
                                        },
                                        {
                                            xtype: 'htmleditor',
                                            name: 'Content',
                                            itemId: 'txtContent',
                                            stateful: true,
                                            fieldLabel: '指令说明',
                                            height: 280,
                                            allowBlank: false,
                                            autoScroll: false,
                                            fontFamilies: ['宋体', '新宋体', '黑体', '楷体', '隶书', '幼圆', '仿宋']
                                        },
                                        {
                                            xtype: 'radiogroup',
                                            fieldLabel: '指令类型',
                                            columns: 6,
                                            items:
                                            [
                                                {
                                                    boxLabel: '工程指令',
                                                    name: 'InstructionType',
                                                    inputValue: 0
                                                },
                                                {
                                                    boxLabel: '养护指令',
                                                    name: 'InstructionType',
                                                    inputValue: 1
                                                }
                                            ]
                                        },
                                        {
                                            xtype: 'radiogroup',
                                            fieldLabel: '发送范围',
                                            columns: 6,
                                            items:
                                            [
                                                {
                                                    boxLabel: '所有单位',
                                                    name: 'PostType',
                                                    inputValue: 0,
                                                    checked: true,
                                                    listeners:
                                                    {
                                                        //处理变化事件
                                                        change: function (field, newValue, oldValue, eOpts)
                                                        {
                                                            //获得控件
                                                            var chkReceivers = field.up('form').down('#chkReceivers');

                                                            //设置控件状态
                                                            chkReceivers.setDisabled(newValue);
                                                        }
                                                    }
                                                },
                                                {
                                                    boxLabel: '指定单位',
                                                    name: 'PostType',
                                                    inputValue: 1
                                                }
                                            ]
                                        },
                                        {
                                            xtype: 'checkboxgroup',
                                            itemId: 'chkReceivers',
                                            fieldLabel: '接收单位',
                                            disabled: true,
                                            border: 1,
                                            columns: 4,
                                            vertical: false,
                                            autoScroll: true,
                                            items: master_organization,
                                            height: 130,
                                            maxHeight: 130
                                        }
                                    ],
                                    buttons:
                                    [
                                        {
                                            text: '确定',
                                            height: 26,
                                            margin: 3,
                                            handler: function ()
                                            {
                                                //获得窗体对象
                                                var form = this.up('form');

                                                //获得表单对象
                                                var formBase = form.getForm();

                                                //检查数据是否合法
                                                if (formBase.isValid())
                                                {
                                                    //提交数据
                                                    formBase.submit
                                                    (
                                                        {
                                                            url: Common.MakeAbsoluteUrl('Update'),
                                                            method: HttpMethod.Post,
                                                            scope: this.up('window'),
                                                            submitEmptyText: false,
                                                            waitTitle: '请稍候',
                                                            waitMsg: '正在保存数据...',
                                                            success: function (form, action)
                                                            {
                                                                //处理提交错误
                                                                if (Common.HandleFormError(form, action))
                                                                {
                                                                    //重置数据
                                                                    form.reset();
                                                                    //隐藏窗体
                                                                    this.hide();
                                                                    //刷新数据
                                                                    store.load();
                                                                }
                                                            },
                                                            failure: Common.HandleFormFailure
                                                        }
                                                    );
                                                }
                                            }
                                        },
                                        {
                                            text: '取消',
                                            height: 26,
                                            margin: 3,
                                            handler: function ()
                                            {
                                                //获得表单对象
                                                var form = this.up('form').getForm();
                                                //重置数据
                                                form.reset();
                                                //隐藏窗体
                                                this.up('window').hide();
                                            }
                                        }
                                    ]
                                }
                            ]
                        }
                    );
                }

                //设置当前行
                this.win_edit.current_row = selection.data;

                //显示窗体
                this.win_edit.show();
            }
        },

        //处理提交按钮点击事件
        onSubmitClick: function ()
        {
            //获得数据存储对象
            var store = this.store;

            //获得选中的行
            var selection = this.getView().getSelectionModel().getSelection();

            //判断是否选中行
            if (selection.length > 0)
            {
                var instructions = [];

                //循环添加
                for (var index in selection)
                {
                    //添加记录
                    instructions.push(selection[index].data.InstructionID);
                }

                //询问是否确定提交
                Ext.Msg.show
                (
                    {
                        title: '确认发布',
                        width: 320,
                        height: 240,
                        msg: '确定要发布将选中的指令吗?<br/><font color="red">注意：指令一旦提交，则无法修改。</font>',
                        buttons: Ext.Msg.YESNO,
                        icon: Ext.Msg.QUESTION,
                        fn: function (button)
                        {
                            //判断是否确定删除 
                            if (button == 'yes')
                            {
                                //创建请求对象
                                Ext.Ajax.request
                                (
                                    {
                                        url: Common.MakeAbsoluteUrl('ExecuteSubmit'),
                                        method: HttpMethod.Post,
                                        params:
                                        {
                                            instructions: instructions, passed: true
                                        },
                                        callback: function (options, success, response)
                                        {
                                            //处理错误
                                            if (Common.HandleError(options, success, response))
                                            {
                                                //加载数据
                                                store.load();
                                            }
                                        }
                                    }
                                );
                            }
                        }
                    }
                );
            }
        }
    }
);

//挂接环境准备完毕事件
Ext.onReady
(
    function ()
    {
        //初始化快速提示
        Ext.QuickTips.init();

        //设置状态保存提供者（当前设置为Cookie）
        Ext.state.Manager.setProvider( Ext.create( 'Ext.state.CookieProvider', { path: _Current_ActionUrl } ) );

        //初始化附加验证类型
        Common.InitAdditionalValidation();

        //创建数据存储对象
        var store = Ext.create
        (
            'Ext.data.Store',
            {
                pageSize: 20,
                autoSync: true,
                autoLoad: true,
                root: 'data',
                remoteFilter: true,
                remoteSort: true,
                model: 'InstructionModel',
                proxy:
                {
                    type: 'ajax',
                    batchActions: false,
                    url: Common.MakeAbsoluteUrl('SubmitSelect'),
                    reader:
                    {
                        type: 'json',
                        root: 'data'
                    },
                    listeners:
                    {
                        exception: function (proxy, response, operation, options)
                        {
                            //处理错误
                            Common.HandleProxyError(proxy, response, operation, options);
                        }
                    }
                }
            }
        );

        //创建选择模型
        var selectionModel = Ext.create('Ext.selection.CheckboxModel');

        //创建表格
        Ext.create
        (
            'Instruction.Grid',
            {
                store: store,
                selModel: selectionModel,
                stateful: true,
                stateId: 'instruction-submit-state-grid',
                height: 600,
                width: '100%',
                id: 'instruction-grid',
                title: '',
                renderTo: 'data-grid',
                columnLines: true,
                viewConfig:
                {
                    stripeRows: true,
                    loadingText: '数据加载中...',
                    singleSelect: true
                },
                //创建分页工具栏
                bbar: Ext.create
                (
                    'Ext.PagingToolbar',
                    {
                        store: store,
                        displayInfo: true,
                        displayMsg: '第 {0} - 第 {1} 条记录，共 {2} 条记录',
                        emptyMsg: ''
                    }
                ),
                columns:
                [
                    {
                        text: '指令主题',
                        width: 240,
                        sortable: false,
                        align: 'left',
                        dataIndex: 'Title',
                        hideable: false
                    },
                    {
                        text: '状态',
                        width: 80,
                        sortable: false,
                        hideable: false,
                        align: 'center',
                        renderer: Common.RenderDataState,
                        dataIndex: 'State'
                    },
                    {
                        text: '指令类型',
                        width: 80,
                        sortable: false,
                        hideable: false,
                        align: 'center',
                        renderer: Common.RenderInstructionType,
                        dataIndex: 'InstructionType'
                    },
                    {
                        text: '发布方式',
                        width: 80,
                        sortable: false,
                        hideable: false,
                        align: 'center',
                        renderer: Common.RenderPostType,
                        dataIndex: 'PostType'
                    },
                    {
                        text: '发布人',
                        width: 120,
                        sortable: false,
                        hideable: false,
                        align: 'left',
                        dataIndex: 'Creater'
                    },
                    {
                        text: '发布日期',
                        width: 80,
                        sortable: true,
                        hideable: false,
                        align: 'right',
                        renderer: Ext.util.Format.dateRenderer('Y/m/d'),
                        dataIndex: 'CreateDate'
                    },
                    {
                        text: '',
                        flex: true,
                        sortable: false,
                        align: 'left',
                        hideable: false
                    }
                ]
            }
        );

        $(window).bind
        (
            "resize",
            function ()
            {
                //调整表格宽度
                Common.AdjustGridWidth('instruction-grid', 'body_content');
            }
        );

        //调整表格宽度
        Common.AdjustGridWidth('instruction-grid', 'body_content');
    }
);
